package com.lyncs.ods.modules.txn.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.modules.goods.model.SkuInfo;
import com.lyncs.ods.modules.goods.service.SkuInfoService;
import com.lyncs.ods.modules.txn.model.TxnSkuDetail;
import com.lyncs.ods.modules.txn.mapper.TxnSkuDetailMapper;
import com.lyncs.ods.modules.txn.model.TxnSkuDetailVO;
import com.lyncs.ods.modules.txn.service.TxnSkuDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * SKU交易详情 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@Service
public class TxnSkuDetailServiceImpl extends ServiceImpl<TxnSkuDetailMapper, TxnSkuDetail> implements TxnSkuDetailService {

    @Autowired
    private SkuInfoService skuInfoService;

    @Override
    public List<TxnSkuDetailVO> getDetails(List<Long> orderIds, List<Long> deliverIds) {
        List<TxnSkuDetail> txnSkuDetails = getTxnSkuDetails(orderIds, deliverIds);
        Set<Long> skuIds = txnSkuDetails.stream().map(TxnSkuDetail::getSkuId).collect(Collectors.toSet());
        List<SkuInfo> skuInfos = skuInfoService.lambdaQuery().in(SkuInfo::getId, skuIds).list();
        Map<Long, SkuInfo> skuInfoMap = skuInfos.stream().collect(Collectors.toMap(SkuInfo::getId, o -> o, (o1, o2) -> o1));
        return txnSkuDetails.stream().map(detail -> new TxnSkuDetailVO().setTxnSkuDetail(detail).setSkuInfo(skuInfoMap.get(detail.getSkuId()))).collect(Collectors.toList());
    }

    @Override
    public List<TxnSkuDetail> getTxnSkuDetail(Long orderId, Long deliverId) {
        return getTxnSkuDetail(orderId, deliverId, Boolean.FALSE);
    }

    @Override
    public List<TxnSkuDetail> getTxnSkuDetail(Long orderId, Long deliverId, Boolean containDeletedSku) {
        LambdaQueryChainWrapper<TxnSkuDetail> wrapper = lambdaQuery().ne(!containDeletedSku, TxnSkuDetail::getStatus, LyncsOdsConstant.OrderStatus.DELETED.getKey());
        //订单查详情
        if (deliverId == null) {
            wrapper.isNull(TxnSkuDetail::getDeliverId)
                    .eq(TxnSkuDetail::getOrderId, orderId);
        } else {
            //交付单查详情
            wrapper.eq(orderId != null, TxnSkuDetail::getOrderId, orderId).eq(TxnSkuDetail::getDeliverId, deliverId);
        }
        return wrapper.list();
    }

    @Override
    public List<TxnSkuDetail> getTxnSkuDetails(List<Long> orderIds, List<Long> deliverIds) {
        if (CollectionUtil.isEmpty(orderIds) && CollectionUtil.isEmpty(deliverIds)) {
            return null;
        }
        LambdaQueryChainWrapper<TxnSkuDetail> wrapper = lambdaQuery().ne(TxnSkuDetail::getStatus, LyncsOdsConstant.TxnSkuStatus.DELETED.getKey());
        //订单查详情
        if (CollectionUtil.isEmpty(deliverIds)) {
            wrapper.isNull(TxnSkuDetail::getDeliverId).in(TxnSkuDetail::getOrderId, orderIds);
        } else {
            //交付单查详情
            wrapper.in(TxnSkuDetail::getDeliverId, deliverIds);
        }
        return wrapper.list();
    }

    @Override
    public Integer getOrderDeliverCount(@NotNull Long orderId) {
        return this.baseMapper.getOrderDeliverCount(orderId);
    }
}
